#define _CRT_SECURE_NO_WARNINGS 1

struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    struct ListNode* tailA = headA;
    struct ListNode* tailB = headB;
    int lenA = 1, lenB = 1;
    while (tailA->next)
    {
        tailA = tailA->next;
        lenA++;
    }
    while (tailB->next)
    {
        tailB = tailB->next;
        lenB++;
    }
    if (tailA != tailB)
        return NULL;

    int gas = abs(lenA - lenB);
    struct ListNode* taillong = headA;
    struct ListNode* tailshort = headB;
    if (lenA < lenB)
    {
        taillong = headB;
        tailshort = headA;
    }
    while (gas--)
    {
        taillong = taillong->next;
    }
    while (taillong != tailshort)
    {
        taillong = taillong->next;
        tailshort = tailshort->next;
    }
    return tailshort;
}